<?php

namespace App\Http\Controllers\Web;

use App\Models\BaseModel;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\View;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    protected $pageNum = 20;
    protected $query;//查询条件

    public function __construct(Request $request)
    {

        $this->setView();

        View::share('pageTitle', '');

        $this->setQuery();
    }

    private function setView()
    {
        $request = request();

        $this->pageNum = (int)$request->input('page_num', 20);
        $this->pageNum = $this->pageNum <= 0 ? 20 : $this->pageNum;

        View::share('pageNums', config('common.page_nums'));
        View::share('currentPageNum', $this->pageNum);

    }

    /**
     * 设置后台列表查询条件
     */
    protected function setQuery()
    {
        //搜索条件
        $this->query = (array)request()->get('query');

        foreach ($this->query as $key => $value) {
            if (is_numeric($value) && !$value) {
                $this->query[$key] = intval($value);
            }
        }
        View::share('query', $this->query);
    }

    /**
     * 设置页面标题
     */
    protected function setPageTitle($title)
    {
        View::share('pageTitle', $title);
    }


    /**
     * 成功返回
     * @param string $message
     * @param string $data
     * @param string $redirect
     * @return array|string
     */
    public function success($data = '', $message = 'ok',$redirect='')
    {
        response()->json([
            'status' => 200,
            'data' => $data,
            'message' => $message,
            'time' => time(),
            'errno' => 0,
            'redirect' => $redirect
        ])->send();
    }

    /**
     * 失败返回
     * $errno
     * @param $errno
     * @param string $message
     * @param string $data
     * @return array|string
     */
    public function error($status, $message = '系统错误', $data = '')
    {
        response()->json([
            'status' => $status,
            'data' => $data,
            'message' => $message,
            'time' => time(),
            'errno' => $status
        ], 200, ['charset' => 'utf-8'])->send();
    }

    /**
     * @param $request
     * @param $instance
     * @param string $scenario
     * @return mixed
     */
    protected function validateScenario($request, $instance, $scenario = 'default')
    {
        $rules = $instance->getRules($request, $scenario);

        $this->validate($request, $rules, $instance->getMessages($scenario));
        $validate = $instance->getScenarios($scenario);
        $data = $request->only($validate);

        $data = $instance->validateFilter($data, $scenario);
        return $data;
    }

    /**
     * 验证，只验证配置字段，其他字段放行
     * @param $request
     * @param $instance
     * @param string $scenario
     * @return mixed
     */
    protected function validateScenarioPass($request, $instance, $scenario = 'default')
    {
        $rules = $instance->getRules($request, $scenario);

        $this->validate($request, $rules, $instance->getMessages($scenario));
        $validate = $instance->getScenarios($scenario);

        $data = $request->all($validate);

        $allkey = array_keys($data);
        foreach ($allkey as $value){
            if(strstr($value, '/')){
                unset($data[$value]);
            }
        }

        unset($data['_token'], $data['s'], $data['_method']);
        $data = $instance->validateFilter($data, $scenario);

        return $data;
    }

}
